package com.dinadi.yidi.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.SpecialAdapter;
import com.dinadi.yidi.base.BaseFragment;
import com.dinadi.yidi.bean.MessageBean;
import com.dinadi.yidi.bean.SpecialRequest;
import com.dinadi.yidi.bean.SpecialRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.ui.view.ZoomOutPageTransformer;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.TLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hc-3020-i3 on 2016/10/28.   首页
 */

public class SpecialFragment extends BaseFragment {
    private View view;
    private ViewPager mViewPager;
    private SpecialAdapter mPagerAdapter;
    private TextView page_size, page_num;
    private List<SpecialRequestEntity> entityList = new ArrayList<>();
    private String token;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TLog.error("SpecialFragment---onCreate");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        TLog.error("SpecialFragment---onDestroy");
    }

    /**
     * 当出现无网络界面时，刷新界面使用
     */
    @Override
    protected void noWifiRefresh() {
        TLog.error("重新刷新数据noWifiRefresh()");
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        CommonInterface specialInterface = new CommonInterface(getActivity(), httpListener);
        specialInterface.getInterface("14001", JSON.toJSONString(bean));
        page_num.setText("1");
    }

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_special, null);
        return view;
    }


    @Override
    protected void initView() {
        changePager(2);
        page_size = (TextView) view.findViewById(R.id.page_size);
        page_num = (TextView) view.findViewById(R.id.page_num);
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        /*viewpager之间的距离*/
        mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.dp_10));
        mPagerAdapter = new SpecialAdapter(getActivity());
        mViewPager.setAdapter(mPagerAdapter);
//        mViewPager.setOffscreenPageLimit(3);
    }

    @Override
    protected void initListener() {

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                page_num.setText(position + 1 + "");
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    @Override
    protected void clickEvent(View v) {

    }

    @Override
    protected void initData() {
        String userId = SharePreferencesHelper.getInstance(getActivity()).getString("userId");
        token = SharePreferencesHelper.getInstance(getActivity()).getString("token");
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        CommonInterface specialInterface = new CommonInterface(getActivity(), httpListener);
        specialInterface.getInterface("14001", JSON.toJSONString(bean));
        page_num.setText("1");
    }


    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            changePager(2);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                SpecialRequest bean = JsonUtil.parseJsonToBean(response, SpecialRequest.class);
                entityList = bean.getSpecialList();
                String pageSize = entityList.size() + "";
                page_size.setText(pageSize);
                mPagerAdapter.addAll(entityList);
            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            changePager(1);
            closeLoadingDialog();
            super.OnFail(errorResponse);

        }
    };
}
